import { createAction } from 'redux-actions'
import { check, body,delJson, getJson, postJson, parseJSON, checkStatus,checkCallback, data, putJson } from './util'
import { success } from '../ui-components'
import util from '../util'


export const fetchNavs = createAction('fetch review navs', () =>
  fetch(`/sidebars/review`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(data)
)
export const fetchPendingPeerReviews = createAction('fetch pending peer reviews', (arg, cb) =>
  fetch(`/reviews/invitations/unsubmitted?${util.body(arg)}`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => {
      cb && cb()
      return res.data
    })
)
export const fetchCanceledPeerReviews = createAction('fetch canceled peer reviews', (arg, cb) =>
  fetch(`/reviews/invitations/canceled?${util.body(arg)}`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => {
      cb && cb()
      return res.data
    })
)
export const fetchFinishedPeerReviews = createAction('fetch finished peer reviews', (arg, cb) =>
  fetch(`/reviews/invitations/submitted?${util.body(arg)}`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => {
      cb && cb()
      return res.data
    })
)

export const refuseInvitation = createAction('refuse review invitation', (invitationId, cb) =>
  fetch(`/reviews/invitations/${invitationId}/refuse`, postJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => {
      cb()
      return res.data
    })
)
export const cancelInvitation = createAction('cancel review invitation', ({invitationId}, options) =>
  fetch(`/reviews/invitations/${invitationId}/cancel-submission`, postJson)
    .then(res => checkCallback(res, options))
)

export const fetchPendingSelfReviews = createAction('fetch pending self reviews', (arg, cb) =>
  fetch(`/reviews/activities/unsubmitted?${util.body(arg)}`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => {
      cb && cb(res.data);
      return res.data
    })
)
export const fetchFinishedSelfReviews = createAction('fetch finished self reviews', (arg, cb) =>
  fetch(`/reviews/activities/others?${util.body(arg)}`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => {
      cb && cb()
      return res.data
    })
)



export const fetchReviewItem = createAction('fetch review item', (itemId, viewMode) => {
  const url = viewMode == 'reports' ? `/reviews/activities/${itemId}/overview` : `/reviews/${viewMode}/${itemId}/detail`
  return fetch(url, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => ({
      [viewMode == 'invitations' ? 'invitationId' : 'activityId']: itemId,
      ...res.data
    }))
})
export const fetchReviewItemProjects = createAction('fetch review item projects', (itemId, viewMode) => {
  const url = viewMode == 'reports' ? `/reviews/activities/${itemId}/overview` : `/reviews/${viewMode}/${itemId}/detail`
  return fetch(url, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => ({
      ...res.data
    }))
})
export const fetchReviewItemInvitations = createAction('fetch review item invitations', (itemId, viewMode) =>
  fetch(`/reviews/${viewMode}/${itemId}/invitedUsers`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => {
      return { [viewMode == 'invitations' ? 'invitationId' : 'activityId']: itemId, ...res.data }
    })
)
export const clearReviewItem = createAction('clear review item')

export const updateReviewItemInvitations = createAction('update review item invitations', (activityId, post, cb) =>
  fetch(`/reviews/activities/${activityId}/invitedUsers`, {
    ...postJson,
    ...body({...post})
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => {
      cb()
      return res.data
    })
)


export const createReviewProject = createAction('create review project', (activityId, post, cb) =>
  fetch(`/reviews/activities/${activityId}/projects`, {
    ...postJson,
    ...body({...post})
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => {
      cb()
      return res.data
    })
)

export const updateReviewProject = createAction('update review project', (activityId, projectId, post, cb) =>
  fetch(`/reviews/activities/${activityId}/projects/${projectId}`, {
    ...putJson,
    ...body({...post})
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => {
      cb()
      return {
        projectId,
        ...post,
      }
    })
)


export const deleteReviewProject = createAction('delete review project', (itemId, projectId, cb) =>
  fetch(`/reviews/activities/${itemId}/projects/${projectId}`, delJson)
  .then(checkStatus)
  .then(parseJSON)
  .then(check)
  .then(res => {
    cb()
    return projectId;
  })
)


export const addRevieweeComment = createAction('add reviewee comment', (activityId, questionId, post, cb) =>
  fetch(`/reviews/activities/${activityId}/questions/${questionId}/comments`, {
    ...postJson,
    ...body({...post})
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(({data}) => {
      cb && cb()
      return {
        questionId,
        data,
        post
      }
    })
)

export const updateRevieweeComment = createAction('update reviewee comment', (activityId, questionId, commentId, post,cb) =>
  fetch(`/reviews/activities/${activityId}/questions/${questionId}/comments/${commentId}`, {
    ...putJson,
    ...body({...post})
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(({data}) => {
      cb && cb();
      return {
        activityId,
        questionId,
        commentId,
        data: post,
      };
    })
)
export const addReviewerComment = createAction('add reviewer comment', (itemId, questionId, post,cb) =>
  fetch(`/reviews/invitations/${itemId}/questions/${questionId}/comments`, {
    ...postJson,
    ...body({...post})
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(({data}) => {
      cb && cb()
      return {
        questionId,
        data,
        post
      }
    })
)

export const updateReviewerComment = createAction('update reviewer comment', (invitationId, questionId, commentId, post,cb) =>
  fetch(`/reviews/invitations/${invitationId}/questions/${questionId}/comments/${commentId}`, {
    ...putJson,
    ...body({...post})
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(({data}) => {
      cb && cb();
      return {
        invitationId,
        questionId,
        commentId,
        data: post,
      };
    })
)
export const addReviewerProjectComment = createAction('add reviewer project comment', (itemId, projectId, post,cb) =>
  fetch(`/reviews/invitations/${itemId}/projects/${projectId}/comments`, {
    ...postJson,
    ...body({...post})
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(({data}) => {
      cb && cb();
      return {
        projectId,
        data,
        post
      };
    })
)

export const updateReviewerProjectComment = createAction('update reviewer project comment', (invitationId, projectId, commentId, post, cb) =>
  fetch(`/reviews/invitations/${invitationId}/projects/${projectId}/comments/${commentId}`, {
    ...putJson,
    ...body({...post})
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(({data}) => {
      cb && cb();
      return {
        invitationId,
        projectId,
        commentId,
        data: post,
      };
    })
)

export const deleteReviewProjectComment = createAction('delete review project comment', (itemId, projectId, commentId, cb) =>
  fetch(`/reviews/invitations/${itemId}/projects/${projectId}/comments/${commentId}`, delJson)
  .then(checkStatus)
  .then(parseJSON)
  .then(check)
  .then(res => {
    cb()
    return res.data
  })
)

export const fetchReviewProject = createAction('fetch review project', (itemId, projectId, index, viewMode, cb) => {

  const url = viewMode == 'reports' 
                  ? `/reviews/activities/${itemId}/overview/projects/${projectId}` 
                  : `/reviews/${viewMode}/${itemId}/projects/${projectId}`
  return fetch(url, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => {
      cb && cb(res.data);
      return {
        projectId,
        index,
        ...res.data
      }
    })
})

export const submitSelfReview = createAction('submit self review', (itemId, viewMode, cb) =>
  fetch(`/reviews/${viewMode}/${itemId}/submit`, {
    ...postJson,
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => {
      cb()
      return res.data
    })
)
export const submitPeerReview = createAction('submit self review', (itemId, viewMode, post, cb) =>
  fetch(`/reviews/${viewMode}/${itemId}/submit`, {
    ...postJson,
    ...body({...post})
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => {
      cb()
      return res.data
    })
)


// Templates
export const fetchReviewTemplates = createAction('fetch review templates', (args = {}) =>
  fetch(`/reviews/templates${args.status?'?status='+args.status:''}`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(data)
)

export const fetchReviewReport = createAction('fetch review report', templateId =>
  fetch(`/reviews/templates/${templateId}/report`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => ({
      ...res.data
    }))
)
export const fetchReviewReportActivities = createAction('fetch review report activities', (templateId, userId) =>
  fetch(`/reviews/templates/${templateId}/all-activities`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(data)
)



export const publishReviewTemplate = createAction('publish review template', (templateId, cb) =>
  fetch(`/reviews/templates/${templateId}/publish`, postJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => {
      cb()
      return res.data
    })
)

export const cancelReviewTemplate = createAction('cancel review template', (templateId, cb) =>
  fetch(`/reviews/templates/${templateId}`, delJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => {
      cb()
      return res.data
    })
)

export const createTemplate = createAction('create review template', (post, cb) =>
  fetch('/reviews/templates', {
    ...postJson,
    ...body({...post})
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => {
      cb()
      return {
        ...post,
        state: 1,
        templateId: res.data.idValue
      }
    })
)

export const updateTemplate = createAction('update template', (arg, cb) =>{
  const { templateId, ...others } = arg
  return (
    fetch(`/reviews/templates/${templateId}`, {
      ...putJson,
      ...body(others)
    })
      .then(checkStatus)
      .then(parseJSON)
      .then(check)
      .then(() => {
        cb && cb();
        return arg;
      })
  )
})

export const increaseUnready = createAction('increase unready by one')
export const decreaseUnready = createAction('decrease unready by one')








export const fetchReviewList = createAction('fetch review templates', (arg, cb) =>
  fetch(`/reviews/templates?${util.body(arg)}`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => {
      cb && cb(res.data);
      return res.data;
    })
)
export const getActivities = createAction('void', (arg, cb) => {
  const {
    templateId,
    ...others
  } = arg
  return fetch(`/reviews/templates/${templateId}/activities?${util.body(others)}`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => {
      cb && cb(res.data);
    })
});

export const getReviewer = createAction('void', (arg, cb) => {
  const {
    templateId,
    ...others
  } = arg;
  return fetch(`/reviews/templates/${templateId}/invitations-as-reviewer?${util.body(others)}`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => {
      cb && cb(res.data);
    })
});
export const cleanReviewList = createAction('clear review item');

//获取首页列表
export const fetchSelfReviewList = createAction('fetch self review templates', (arg, cb) =>
  fetch(`/reviews/templates/activities-for-homepage`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => {
      return res.data;
    })
)

export const fetchPeerReviewList = createAction('fetch peer review templates', (arg, cb) =>
  fetch(`/reviews/templates/invitations-for-homepage`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(res => {
      return res.data;
    })
)












